package com.amikomgamedev.panjatpinang.states;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.input.touch.detector.SurfaceScrollDetector;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.content.Intent;
import android.view.KeyEvent;

import com.amikomgamedev.panjatpinang.Data;
import com.amikomgamedev.panjatpinang.Data_Credits;
import com.amikomgamedev.panjatpinang.Define;
import com.amikomgamedev.panjatpinang.Utils;

/*
 * AndEngineGamePanjatPinang
 * State_Credit.java
 * Pratama Wijaya
 * set.mnemonix@gmail.com
 * 2:37:25 PM , Aug 5, 2012
 */

public class State_Credit extends SimpleBaseGameActivity implements Data,
		Define, Data_Credits {

	private Camera					mCamera;
	private Scene					mScene;

	private BitmapTextureAtlas		texBg, texFontMed, texFontSmall;
	private ITextureRegion			treBg;
	private Sprite					sprBg;

	private BitmapTextureAtlas		texJudul;
	private ITextureRegion			treJudul;
	private Sprite					sprJudul;

	private BitmapTextureAtlas		texTmptTulisan, texLogoAgd;
	private ITextureRegion			treLogoAgd;
	private Sprite					sprLogoAgd;

	private HUD						hudMainHud;

	private static ScrollDetector	mScrollDetector;
	private static boolean			manualScrolling;

	private static Text				txtPM, txtPMname, txtGameDesigner,
			txtGameDesignerName, txtProgrammer, txtProgrammerName, txtArtist,
			txtArtisName1, txtArtisName2, txtArtisName3, txtArtisName4,
			txtSoundEngineer, txtSoundEngineer1, txtSoundEngineer2,
			txtSpecialThanks, txtSpecialThanksName1, txtSupportBy,
			txtSupportByName1, txtCopyright, txtCopyrightName1,
			txtCopyrightName2;
	private static Font				fontMedium, fontSmall;

	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		mCamera = new Camera(0, 0, GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
		EngineOptions en = new EngineOptions(true,
				ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(
						GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT), mCamera);
		return en;
	}

	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub

		texLogoAgd = new BitmapTextureAtlas(this.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		treLogoAgd = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				texLogoAgd, this.getAssets(), IMG_LOGO_AGD_CREDIT, 0, 0);

		texJudul = new BitmapTextureAtlas(this.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		treJudul = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				texJudul, this.getAssets(), IMG_LOGO_PANJAT_PINANG, 0, 0);

		texBg = new BitmapTextureAtlas(this.getTextureManager(), 512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		treBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texBg,
				this.getAssets(), IMG_BG_CREDIT, 0, 0);

		texFontMed = new BitmapTextureAtlas(this.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR);
		texFontSmall = new BitmapTextureAtlas(this.getTextureManager(), 256,
				256, TextureOptions.BILINEAR);

		fontMedium = FontFactory.createFromAsset(this.getFontManager(),
				texFontMed, this.getAssets(), FONT_STYLE, FONT_SIZE_MEDIUM,
				true, android.graphics.Color.BLACK);
		fontSmall = FontFactory.createFromAsset(this.getFontManager(),
				texFontSmall, this.getAssets(), FONT_STYLE, FONT_SIZE_SMALL,
				true, android.graphics.Color.BLACK);

		
		texLogoAgd.load();
		texBg.load();
		texJudul.load();
		fontMedium.load();
		fontSmall.load();

	}

	@Override
	protected Scene onCreateScene() {
		// TODO Auto-generated method stub
		mEngine.registerUpdateHandler(new FPSLogger());
		mScene = new Scene();
		hudMainHud = new HUD();

		manualScrolling = false;

		sprBg = new Sprite(0, 0, treBg, mEngine.getVertexBufferObjectManager());

		mScene.attachChild(sprBg);

		sprJudul = new Sprite(0, 0, treJudul,
				mEngine.getVertexBufferObjectManager());
		sprJudul.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - sprJudul.getWidth() / 2, 100);

		// Project Manager

		txtPM = new Text(0, 0, fontMedium, TXT_PROJECT_MANAGER,
				mEngine.getVertexBufferObjectManager());
		txtPM.setPosition((GAME_SCREEN_WIDTH >> 1) - txtPM.getWidth() / 2,
				sprJudul.getY() + sprJudul.getHeight() + 40);

		txtPMname = new Text(0, 0, fontSmall, TXT_PROJECT_MANAGER_NAME,
				mEngine.getVertexBufferObjectManager());
		txtPMname.setPosition((GAME_SCREEN_WIDTH >> 1) - txtPMname.getWidth()
				/ 2, txtPM.getY() + txtPM.getHeight() + 10);

		// game designer
		txtGameDesigner = new Text(0, 0, fontMedium, TXT_GAME_DESIGNER,
				mEngine.getVertexBufferObjectManager());
		txtGameDesigner.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - (txtGameDesigner.getWidth() / 2),
				txtPMname.getY() + txtPMname.getHeight() + 40);
		txtGameDesignerName = new Text(0, 0, fontSmall, TXT_GAME_DESIGNER_NAME,
				mEngine.getVertexBufferObjectManager());
		txtGameDesignerName.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtGameDesignerName.getWidth() / 2,
				txtGameDesigner.getHeight() + txtGameDesigner.getY() + 10);

		// programmer

		txtProgrammer = new Text(0, 0, fontMedium, TXT_PROGRAMMER,
				mEngine.getVertexBufferObjectManager());
		txtProgrammer.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtProgrammer.getWidth() / 2,
				txtGameDesignerName.getHeight() + txtGameDesignerName.getY()
						+ 40);
		txtProgrammerName = new Text(0, 0, fontSmall, TXT_PROGRAMMER_NAME,
				mEngine.getVertexBufferObjectManager());
		txtProgrammerName.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtProgrammerName.getWidth() / 2, txtProgrammer.getHeight()
				+ txtProgrammer.getY() + 10);

		// 2D Artist
		txtArtist = new Text(0, 0, fontMedium, TXT_2D_ARTIST,
				mEngine.getVertexBufferObjectManager());
		txtArtist.setPosition((GAME_SCREEN_WIDTH >> 1)
				- (txtArtist.getWidth() / 2), txtProgrammerName.getY()
				+ txtProgrammerName.getHeight() + 40);

		txtArtisName1 = new Text(0, 0, fontSmall, TXT_2D_ARTIST_NAME1,
				mEngine.getVertexBufferObjectManager());
		txtArtisName1.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtArtisName1.getWidth() / 2,
				txtArtist.getHeight() + txtArtist.getY() + 10);

		txtArtisName2 = new Text(0, 0, fontSmall, TXT_2D_ARTIST_NAME2,
				mEngine.getVertexBufferObjectManager());
		txtArtisName2.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtArtisName2.getWidth() / 2,
				txtArtisName1.getHeight() + txtArtisName1.getY() + 10);

		txtArtisName3 = new Text(0, 0, fontSmall, TXT_2D_ARTIST_NAME3,
				mEngine.getVertexBufferObjectManager());
		txtArtisName3.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtArtisName3.getWidth() / 2,
				txtArtisName2.getHeight() + txtArtisName2.getY() + 10);

		txtArtisName4 = new Text(0, 0, fontSmall, TXT_2D_ARTIST_NAME4,
				mEngine.getVertexBufferObjectManager());
		txtArtisName4.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtArtisName4.getWidth() / 2,
				txtArtisName3.getHeight() + txtArtisName3.getY() + 10);

		// Sound Engineer
		txtSoundEngineer = new Text(0, 0, fontMedium, TXT_SOUND_ENGINEER,
				mEngine.getVertexBufferObjectManager());
		txtSoundEngineer.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSoundEngineer.getWidth() / 2, txtArtisName4.getY()
				+ txtArtisName4.getHeight() + 40);

		txtSoundEngineer1 = new Text(0, 0, fontSmall, TXT_SOUND_ENGINEER_NAME1,
				mEngine.getVertexBufferObjectManager());
		txtSoundEngineer1.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSoundEngineer1.getWidth() / 2,
				txtSoundEngineer.getHeight() + txtSoundEngineer.getY() + 10);

		txtSoundEngineer2 = new Text(0, 0, fontSmall, TXT_SOUND_ENGINEER_NAME2,
				mEngine.getVertexBufferObjectManager());
		txtSoundEngineer2.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSoundEngineer2.getWidth() / 2,
				txtSoundEngineer1.getHeight() + txtSoundEngineer1.getY() + 10);

		// special thanks
		txtSpecialThanks = new Text(0, 0, fontMedium, TXT_SPECIAL_THANKS,
				mEngine.getVertexBufferObjectManager());
		txtSpecialThanks.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSpecialThanks.getWidth() / 2,
				txtSoundEngineer2.getHeight() + txtSoundEngineer2.getY() + 40);

		txtSpecialThanksName1 = new Text(0, 0, fontSmall,
				TXT_SPECIAL_THANKS_NAME1,
				mEngine.getVertexBufferObjectManager());
		txtSpecialThanksName1.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSpecialThanksName1.getWidth() / 2, txtSpecialThanks.getY()
				+ txtSpecialThanks.getHeight() + 10);

		// supported by
		txtSupportBy = new Text(0, 0, fontMedium, TXT_SUPPORTED_BY,
				mEngine.getVertexBufferObjectManager());
		txtSupportBy.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtSupportBy.getWidth() / 2,
				txtSpecialThanksName1.getY()
						+ txtSpecialThanksName1.getHeight() + 40);

		txtSupportByName1 = new Text(0, 0, fontSmall, TXT_SUPPORTED_BY_NAME1,
				mEngine.getVertexBufferObjectManager());
		txtSupportByName1.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtSupportByName1.getWidth() / 2, txtSupportBy.getHeight()
				+ txtSupportBy.getY() + 10);

		// copyright
		txtCopyright = new Text(0, 0, fontMedium, TXT_COPYRIGHT,
				mEngine.getVertexBufferObjectManager());
		txtCopyright.setPosition(
				(GAME_SCREEN_WIDTH >> 1) - txtCopyright.getWidth() / 2,
				txtSupportByName1.getHeight() + txtSupportByName1.getY() + 40);

		txtCopyrightName1 = new Text(0, 0, fontSmall, TXT_COPYRIGHT_NAME1,
				mEngine.getVertexBufferObjectManager());
		txtCopyrightName1.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtCopyrightName1.getWidth() / 2, txtCopyright.getHeight()
				+ txtCopyright.getY() + 10);
		txtCopyrightName2 = new Text(0, 0, fontSmall, TXT_COPYRIGHT_NAME2,
				mEngine.getVertexBufferObjectManager());
		txtCopyrightName2.setPosition((GAME_SCREEN_WIDTH >> 1)
				- txtCopyrightName2.getWidth() / 2,
				txtCopyrightName1.getHeight() + txtCopyrightName1.getY() + 10);

		sprLogoAgd = new Sprite(0, 0, treLogoAgd,
				mEngine.getVertexBufferObjectManager());
		sprLogoAgd.setPosition((GAME_SCREEN_WIDTH >> 1) - sprLogoAgd.getWidth()
				/ 2, txtCopyrightName2.getY() + txtCopyrightName2.getHeight()
				+ 40);

		hudMainHud.attachChild(sprJudul);
		hudMainHud.attachChild(txtPM);
		hudMainHud.attachChild(txtPMname);

		hudMainHud.attachChild(txtGameDesigner);
		hudMainHud.attachChild(txtGameDesignerName);

		hudMainHud.attachChild(txtProgrammer);
		hudMainHud.attachChild(txtProgrammerName);

		hudMainHud.attachChild(txtArtist);
		hudMainHud.attachChild(txtArtisName1);
		hudMainHud.attachChild(txtArtisName2);
		hudMainHud.attachChild(txtArtisName3);
		hudMainHud.attachChild(txtArtisName4);

		hudMainHud.attachChild(txtSoundEngineer);
		hudMainHud.attachChild(txtSoundEngineer1);
		hudMainHud.attachChild(txtSoundEngineer2);

		hudMainHud.attachChild(txtSpecialThanks);
		hudMainHud.attachChild(txtSpecialThanksName1);

		hudMainHud.attachChild(txtSupportBy);
		hudMainHud.attachChild(txtSupportByName1);

		hudMainHud.attachChild(txtCopyright);
		hudMainHud.attachChild(txtCopyrightName1);
		hudMainHud.attachChild(txtCopyrightName2);
		hudMainHud.attachChild(sprLogoAgd);

		hudMainHud
				.setPosition(hudMainHud.getScaleCenterX(), GAME_SCREEN_HEIGHT);

		mCamera.setHUD(hudMainHud);

		mScene.registerUpdateHandler(new IUpdateHandler() {

			@Override
			public void reset() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onUpdate(float pSecondsElapsed) {
				// TODO Auto-generated method stub

				if (manualScrolling == false) {
					if (hudMainHud.getY() < -(sprLogoAgd.getY() + sprLogoAgd
							.getHeight())) {

						// set ke bawah..

						hudMainHud.setPosition(hudMainHud.getX(),
								GAME_SCREEN_HEIGHT);
					} else {
						Utils.TRACE("masuk 2" + hudMainHud.getY());
						// ini yang bikin naik keatas..
						hudMainHud.setPosition(
								hudMainHud.getX(),
								hudMainHud.getY()
										- (float) getDropDistance(8,
												pSecondsElapsed));
					}

				}

			}
		});

		mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {

			@Override
			public boolean onSceneTouchEvent(Scene pScene,
					TouchEvent pSceneTouchEvent) {
				// TODO Auto-generated method stub
				if (pSceneTouchEvent.isActionUp()) {
					manualScrolling = false;
					return true;
				} else {
					manualScrolling = true;
					mScrollDetector.onTouchEvent(pSceneTouchEvent);
					return true;
				}
			}
		});

		mScrollDetector = new SurfaceScrollDetector(
				new IScrollDetectorListener() {

					@Override
					public void onScroll(ScrollDetector pScollDetector,
							int pPointerID, float pDistanceX, float pDistanceY) {
						// TODO Auto-generated method stub

						if (hudMainHud.getY() < -(sprLogoAgd.getY() + sprLogoAgd
								.getHeight())
								|| hudMainHud.getY() > GAME_SCREEN_HEIGHT) {

							hudMainHud.setPosition(hudMainHud.getX(),
									GAME_SCREEN_HEIGHT);
						} else {
							hudMainHud.setPosition(hudMainHud.getX(),
									hudMainHud.getY() + pDistanceY);
						}

					}

					@Override
					public void onScrollStarted(ScrollDetector pScollDetector,
							int pPointerID, float pDistanceX, float pDistanceY) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onScrollFinished(ScrollDetector pScollDetector,
							int pPointerID, float pDistanceX, float pDistanceY) {
						// TODO Auto-generated method stub

					}
				});
		mScrollDetector.setEnabled(true);

		mScene.setOnSceneTouchListener(this.mScene.getOnSceneTouchListener());
		mScene.setOnSceneTouchListenerBindingOnActionDownEnabled(true);

		return mScene;
	}

	@Override
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent menu = new Intent(State_Credit.this, State_MainMenu.class);
			startActivity(menu);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	public static double getDropDistance(double dropRate, float mSeconds) {
		return (GAME_SCREEN_HEIGHT * mSeconds) / dropRate;
	}

}
